package System;


import com.mysql.cj.jdbc.MysqlDataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-02-03
 * Time: 02:14
 */
public class ShowOperation implements IOperation{
    public void work() throws SQLException {
        System.out.println("显示图书!");

        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/BookControlSystem?characterEncoding=utf8&useSSL=False");
        dataSource.setUser("root");
        dataSource.setPassword("123456");

        Connection connection = dataSource.getConnection();

        String sql = "select * from BookList";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()){
            String name = resultSet.getString(1);
            String author = resultSet.getString(2);
            int price = resultSet.getInt(3);
            String type = resultSet.getString(4);
            String isBorrow = resultSet.getString(5);
            System.out.println("name="+name+"   author="+author+"   price="+price+"   type="+type+"   isBorrow="+isBorrow);
        }
    }
}
